﻿using System;
using System.IO;
using UnityEngine;
using UnityEngine.Events;

namespace RenderHeads.Media.AVProVideo.Demos
{
	// Token: 0x02000009 RID: 9
	public class FrameExtract : MonoBehaviour
	{
		// Token: 0x06000028 RID: 40 RVA: 0x00003110 File Offset: 0x00001310
		private void Start()
		{
			this._mediaPlayer.Events.AddListener(new UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>(this.OnMediaPlayerEvent));
			this._displaySheet = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
			this._displaySheet.useMipMap = false;
			this._displaySheet.autoGenerateMips = false;
			this._displaySheet.antiAliasing = 1;
			this._displaySheet.Create();
			RenderTexture.active = this._displaySheet;
			GL.Clear(false, true, Color.black, 0f);
			RenderTexture.active = null;
		}

		// Token: 0x06000029 RID: 41 RVA: 0x000031A4 File Offset: 0x000013A4
		public void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
		{
			if (et != MediaPlayerEvent.EventType.MetaDataReady)
			{
				if (et == MediaPlayerEvent.EventType.FirstFrameReady)
				{
					this.OnNewMediaReady();
				}
			}
			else
			{
				mp.Play();
				mp.Pause();
			}
		}

		// Token: 0x0600002A RID: 42 RVA: 0x000031D4 File Offset: 0x000013D4
		private void OnNewMediaReady()
		{
			IMediaInfo info = this._mediaPlayer.Info;
			if (this._texture != null)
			{
				UnityEngine.Object.Destroy(this._texture);
				this._texture = null;
			}
			int videoWidth = info.GetVideoWidth();
			int videoHeight = info.GetVideoHeight();
			this._texture = new Texture2D(videoWidth, videoHeight, TextureFormat.ARGB32, false);
			this._timeStepSeconds = this._mediaPlayer.Info.GetDurationMs() / 1000f / 8f;
			this._filenamePrefix = Path.GetFileName(this._mediaPlayer.m_VideoPath);
		}

		// Token: 0x0600002B RID: 43 RVA: 0x00003268 File Offset: 0x00001468
		private void OnDestroy()
		{
			if (this._texture != null)
			{
				UnityEngine.Object.Destroy(this._texture);
				this._texture = null;
			}
			if (this._displaySheet != null)
			{
				RenderTexture.ReleaseTemporary(this._displaySheet);
				this._displaySheet = null;
			}
		}

		// Token: 0x0600002C RID: 44 RVA: 0x000032BC File Offset: 0x000014BC
		private void Update()
		{
			if (this._texture != null && this._frameIndex >= 0 && this._frameIndex < 8)
			{
				this.ExtractNextFrame();
				this._frameIndex++;
			}
		}

		// Token: 0x0600002D RID: 45 RVA: 0x000032FC File Offset: 0x000014FC
		private void ExtractNextFrame()
		{
			float timeSeconds = (float)this._frameIndex * this._timeStepSeconds;
			this._texture = this._mediaPlayer.ExtractFrame(this._texture, timeSeconds, this._accurateSeek, this._timeoutMs);
			if (this._saveToJPG)
			{
				string text = string.Concat(new object[]
				{
					this._filenamePrefix,
					"-",
					this._frameIndex,
					".jpg"
				});
				GlobalData.Debug_Log("Writing frame to file: " + text);
				File.WriteAllBytes(text, this._texture.EncodeToJPG());
			}
			GL.PushMatrix();
			RenderTexture.active = this._displaySheet;
			GL.LoadPixelMatrix(0f, (float)this._displaySheet.width, (float)this._displaySheet.height, 0f);
			Rect sourceRect = new Rect(0f, 0f, 1f, 1f);
			float num = 8f;
			float num2 = (float)this._displaySheet.width / 8f - num;
			float num3 = num2 / ((float)this._texture.width / (float)this._texture.height);
			float x = (num2 + num) * (float)this._frameIndex;
			Rect screenRect = new Rect(x, (float)this._displaySheet.height / 2f - num3 / 2f, num2, num3);
			Graphics.DrawTexture(screenRect, this._texture, sourceRect, 0, 0, 0, 0);
			RenderTexture.active = null;
			GL.PopMatrix();
			GL.InvalidateState();
		}

		// Token: 0x0600002E RID: 46 RVA: 0x00003480 File Offset: 0x00001680
		private void OnGUI()
		{
			GUI.skin = this._skin;
			if (this._displaySheet != null)
			{
				GUI.DrawTexture(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), this._displaySheet, ScaleMode.ScaleToFit, false);
			}
			float num = 4f * ((float)Screen.height / 1080f);
			GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(num, num, 1f));
			GUILayout.Space(16f);
			GUILayout.BeginHorizontal(new GUILayoutOption[]
			{
				GUILayout.ExpandWidth(true),
				GUILayout.Width((float)Screen.width / num)
			});
			GUILayout.FlexibleSpace();
			if (GUILayout.Button("Start Extracting Frames", new GUILayoutOption[0]))
			{
				this._frameIndex = 0;
				RenderTexture.active = this._displaySheet;
				GL.Clear(false, true, Color.black, 0f);
				RenderTexture.active = null;
			}
			GUILayout.FlexibleSpace();
			GUILayout.EndHorizontal();
		}

		// Token: 0x0400001C RID: 28
		private const int NumFrames = 8;

		// Token: 0x0400001D RID: 29
		public MediaPlayer _mediaPlayer;

		// Token: 0x0400001E RID: 30
		public bool _accurateSeek;

		// Token: 0x0400001F RID: 31
		public int _timeoutMs = 250;

		// Token: 0x04000020 RID: 32
		public GUISkin _skin;

		// Token: 0x04000021 RID: 33
		public bool _saveToJPG;

		// Token: 0x04000022 RID: 34
		private string _filenamePrefix;

		// Token: 0x04000023 RID: 35
		private float _timeStepSeconds;

		// Token: 0x04000024 RID: 36
		private int _frameIndex = -1;

		// Token: 0x04000025 RID: 37
		private Texture2D _texture;

		// Token: 0x04000026 RID: 38
		private RenderTexture _displaySheet;
	}
}
